﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace T.FAS.Runtime.Metadata.Entity
{
    /// <summary>
    /// 元数据集合类
    /// </summary>
    public class TMetaDataCollection: Collection<IMetaData>, ICloneable
    {
        /// <summary>
        /// 默认构造函数。
        /// </summary>
        public TMetaDataCollection()
            : base()
        {
        }

        /// <summary>
        /// 根据id获取元数据
        /// </summary>
        /// <param name="metaDataID">metaDataID</param>
        /// <returns>GSPMetaData</returns>
        public IMetaData this[string metaDataID]
        {
            get
            {
                return this.First(x => string.Compare(x.ID, metaDataID, true) == 0);
            }
            set
            {
                IMetaData item = this.FirstOrDefault(x => string.Compare(x.ID, metaDataID, true) == 0);
                if (item == null)
                {
                    throw new Exception("未找到对应的对象！");
                }
                this[this.IndexOf(item)] = value;
            }
        }

        /// <summary>
        /// 判断是否含有该对象
        /// </summary>
        /// <param name="metaDataID">metaDataID</param>
        /// <returns>是否含有对应的对象</returns>
        public bool Contains(string metaDataID)
        {
            IMetaData item = this.FirstOrDefault(x => string.Compare(x.ID, metaDataID, true) == 0);
            if (item == null)
                return false;
            return true;
        }

        /// <summary>
        /// 重写InsertItem方法
        /// </summary>
        /// <param name="index">索引值</param>
        /// <param name="item">FieldContent对象</param>
        protected override void InsertItem(int index, IMetaData item)
        {
            IMetaData dataItem = this.FirstOrDefault(x => string.Compare(x.ID, item.ID, true) == 0);
            if (dataItem != null)
                throw new Exception("该对象已经存在，不允许重复插入!");

            base.InsertItem(index, item);
        }

        /// <summary>
        /// 根据id删除
        /// </summary>
        /// <param name="metaDataID">metaDataID</param>
        public void Remove(string metaDataID)
        {
            IMetaData item = this.First(x => string.Compare(x.ID, metaDataID, true) == 0);
            if (item == null)
                throw new Exception("删除的对象不存在!");
            this.RemoveAt(this.IndexOf(item));
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="collection">数据集合</param>
        public void AddRange(params IMetaData[] collection)
        {
            if (collection == null || collection.Length == 0)
                return;
            foreach (IMetaData item in collection)
            {
                this.Add(item);
            }
        }

        /// <summary>
        /// 创建作为当前实例副本的新对象。
        /// </summary>
        /// <returns>作为此实例副本的新对象。</returns>
        public object Clone()
        {
            var newObj = new TMetaDataCollection();
            foreach (IMetaData item in this)
            {
                newObj.Add(item.Clone() as IMetaData);
            }

            return newObj;
        }
    }
}
